/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.Settings;

import com.googlecode.datawander.i18n.I18n;
import com.googlecode.datawander.i18n.I18n.LANGUAGE;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 *
 * @author Marcin Stachniuk
 */
public class Preferences {

    private static final String PROPERTY_FILENAME = "preferences.properties";

    private static final Logger logger = Logger.getLogger(Preferences.class);
    private LANGUAGE lang;

    public LANGUAGE getLang() {
        return lang;
    }

    public void setLang(LANGUAGE lang) {
        this.lang = lang;
    }

    public void load() throws PreferencesException {
        try {
            Properties prop = new Properties();
            prop.load(new FileReader(PROPERTY_FILENAME));
            String langString = prop.getProperty("language");
            if(langString.equals("en")) {
                lang = LANGUAGE.ENGLISH;
            } else {
                lang = LANGUAGE.POLISH;
            }
        } catch (IOException ex) {
            String message = I18n.getString("cannotloadpreferencespropertiesfile");
            logger.debug(message, ex);
            throw new PreferencesException(message);
        }
        
    }

    public void save() throws PreferencesException {
        try {
            Properties prop = new Properties();
            prop.load(new FileReader(PROPERTY_FILENAME));
            if(lang == LANGUAGE.ENGLISH) {
                prop.setProperty("language", "en");
            } else {
                prop.setProperty("language", "pl");
            }
            prop.store(new FileWriter(PROPERTY_FILENAME), null);
        } catch (IOException ex) {
            String message = I18n.getString("cannotsaveinformationaboutnewlanguage");
            logger.debug(message, ex);
            throw new PreferencesException(message);
        }
    }
}
